package com.wyzz.hr.bo.impl;

import java.util.*;
import org.hibernate.criterion.DetachedCriteria;

import com.wyzz.hr.bo.IAttenddailyBO;
import com.wyzz.hr.dao.IAttenddailyDAO;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.shift.AttendDailyHandler;
import com.wyzz.hr.shift.DefaultFilter;
import com.wyzz.hr.shift.ExceptionDataFilter;
import com.wyzz.hr.support.AttendDailyMemory;
import com.wyzz.hr.support.RecordSorter;
import com.wyzz.hr.util.Pager;

public class AttenddailyBOImpl implements IAttenddailyBO {

	public AttenddailyBOImpl() {
	}

	public List searchAttenddaily(Pager pager, String emp, Employee employee,
			Date dayFrom, Date dayTo, Integer searchType, boolean isExport,
			List empList) {
		List emps = empList;
		AttendDailyHandler handler = new AttendDailyHandler();
		handler.setEmpList(emps);
		handler.setEndDate(dayTo);
		handler.setStartDate(dayFrom);
		List records = handler.generateRecords();
		doFilter(records, searchType);
		records = doSort(records, pager);
		if (pager != null)
			return doPages(records, pager);
		else
			return records;
	}

	private void doFilter(List dataList, Integer searchType) {
		ExceptionDataFilter doNothingFilter = new DefaultFilter();
		Iterator iterator = dataList.iterator();
		AttendDailyMemory bean = null;
		do {
			if (!iterator.hasNext())
				break;
			bean = (AttendDailyMemory) iterator.next();
			boolean flag = doNothingFilter.doFilter(bean, searchType);
			if (!flag)
				iterator.remove();
		} while (true);
	}

	private List doSort(List records, Pager pager) {
		if (pager == null)
			return records;
		RecordSorter sorter = new RecordSorter(records);
		String property = null;
		String order = null;
		if (pager.getOrder() != null && pager.getOrder().indexOf("-") > -1) {
			String temp[] = pager.getOrder().split("-");
			property = temp[0];
			order = temp[1];
		}
		sorter.doSort(property, order);
		return records;
	}

	private List doPages(List records, Pager pager) {
		int size = records.size();
		int pageSize = pager.getPageSize();
		pager.init(size, pageSize);
		if (pager.getCurrentPage() == 0)
			pager.setCurrentPage(1);
		int fromIndex = (pager.getCurrentPage() - 1) * pageSize;
		int toIndex = pager.getCurrentPage() * pageSize;
		if (fromIndex < 0)
			fromIndex = 0;
		if (toIndex > size)
			toIndex = size;
		if (pageSize == -1)
			return records;
		else
			return records.subList(fromIndex, toIndex);
	}

	public List searchEmployee(DetachedCriteria dc) {
		return attenddailyDAO.findByCriteria(dc);
	}

	public IAttenddailyDAO getAttenddailyDAO() {
		return attenddailyDAO;
	}

	public void setAttenddailyDAO(IAttenddailyDAO attenddailyDAO) {
		this.attenddailyDAO = attenddailyDAO;
	}

	public List getAttdShiftList() {
		return attdShiftList;
	}

	public void setAttdShiftList(List attdShiftList) {
		this.attdShiftList = attdShiftList;
	}

	private IAttenddailyDAO attenddailyDAO;
	private List attdShiftList;
}
